package com.luom.fsp.utils;

import com.luom.fsp.manager.websocket.WebSocketServer;
import com.luom.fsp.model.NotificationMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 向管理员后台发送消息
 */
@Component
@Slf4j
public class AsyncMessageSender {

    @Async("taskExecutor")
    public void sendOrderMessage(String content) {
        sendMessage("order", content);
    }

    @Async("taskExecutor")
    public void sendSystemMessage(String content) {
        sendMessage("system", content);
    }

    @Async("taskExecutor")
    public void sendStockMessage(String content) {
        sendMessage("stock", content);
    }

    @Async("taskExecutor")
    public void sendOtherMessage(String content) {
        sendMessage("other", content);
    }

    private void sendMessage(String type, String content) {
        try {
            NotificationMessage message = new NotificationMessage();
            message.setMessageType(type);
            message.setContent(content);
            message.setTimestamp(System.currentTimeMillis());
            WebSocketServer.broadcast(message);
        } catch (Exception e) {
            log.error("消息发送失败: {}", e.getMessage());
        }
    }
}

